﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Resources;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Schema;

public class gf {

	public static void CreateResourceFile( string resxFileName, Control.ControlCollection col ) {
		ResXResourceWriter resxWriter;
		try {
			resxWriter = new ResXResourceWriter( resxFileName );
			foreach ( Control item in col ) {
				resxWriter.AddResource( item.Name, item.Text );
			}

			resxWriter.Close();
		}
		//if the file path is wrong or dosn't found
		catch ( FileNotFoundException caught ) {
			MessageBox.Show( "Source: " + caught.Source + " Message: " + caught.Message );
		}
	}

	private static bool _isValied = true;

	public static string DatumNaRaganjeOdMaticen( string Maticen ) {
		if ( Maticen != "" ) {
			Maticen.Trim().ToString();
			string den, mesec, godina, godina1, godina2 = string.Empty;

			godina1 = "1";
			godina2 = "2";

			den = Maticen.Substring( 0, 2 );
			mesec = Maticen.Substring( 2, 2 );
			godina = Maticen.Substring( 4, 3 );

			if ( godina.Substring( 0, 1 ) == "0" ) {
				godina = godina2 + godina;
			} else {
				godina = godina1 + godina;
			}
			return godina + mesec + den;
		} else {
			return "";
		}
	}

	public static bool ValidateXml( string xmlPath, string XsdPath ) {
		try {
			XmlReader xsd = new XmlTextReader( XsdPath );
			XmlSchemaSet schema = new XmlSchemaSet();
			schema.Add( null, xsd );

			XmlReaderSettings xmlReadeSettings = new XmlReaderSettings();
			xmlReadeSettings.ValidationType = ValidationType.Schema;
			xmlReadeSettings.Schemas.Add( schema );
			xmlReadeSettings.ValidationEventHandler += new ValidationEventHandler( ValidationCallBack );

			XmlTextReader xmlTextReader = new XmlTextReader( xmlPath );
			XmlReader xmlReader = XmlReader.Create( xmlTextReader, xmlReadeSettings );

			while ( xmlReader.Read() )
				;
			xmlReader.Close();

			Form LogForm = new Form();
			ListView items = new ListView();
			items.Dock = DockStyle.Fill;
			items.Scrollable = true;
			items.View = View.List;
			LogForm.ShowIcon = false;
			items.ForeColor = Color.Red;
			LogForm.Text = "Грешки во генерираниот xml Фајл";
			List<string> Lista = new List<string>();
			Lista = Log.Split( '\t' ).ToList();
			items.Items.Clear();
			if ( Lista.Count > 1 ) {
				foreach ( string item in Lista ) {
					items.Items.Add( item );
				}

				if ( items.Items.Count > 0 ) {
					_isValied = false;
				}

				LogForm.ShowInTaskbar = false;
				LogForm.StartPosition = FormStartPosition.CenterScreen;
				LogForm.Size = new System.Drawing.Size( 740, 480 );
				LogForm.FormBorderStyle = FormBorderStyle.FixedSingle;
				LogForm.Controls.Add( items );
				LogForm.ShowDialog();
				LogForm.Dispose();

				Log = "";
				items.Clear();
			}
		}

		catch ( Exception ex ) {
			_isValied = false;
		}

		return _isValied;
	}

	static string Log = "";

	private static void ValidationCallBack( object sender, ValidationEventArgs args ) {
		if ( args.Severity == XmlSeverityType.Warning )
			Log += "\tПредупредување:" + args.Message;
		else
			Log += "\tГрешка: " + args.Message;
	}

	public static String ConvertToLatinica( String input ) {
		Dictionary<char, char> map = new Dictionary<char, char>{
			{'А','A'},{'а','a'},
			{'Б','B'},{'б','b'},
			{'В','V'},{'в','v'},
			{'Г','G'},{'г','g'},
			{'Д','D'},{'д','d'},
			{'Ѓ','\\'},{'ѓ','|'},
			{'Е','E'},{'е','e'},
			{'Ж','@'},{'ж','`'},
			{'З','Z'},{'з','z'},
			{'Ѕ','Y'},{'ѕ','y'},
			{'И','I'},{'и','i'},
			{'Ј','J'},{'ј','j'},
			{'К','K'},{'к','k'},
			{'Л','L'},{'л','l'},
			{'Љ','Q'},{'љ','q'},
			{'М','M'},{'м','m'},
			{'Н','N'},{'н','n'},
			{'Њ','W'},{'њ','w'},
			{'О','O'},{'о','o'},
			{'П','P'},{'п','p'},
			{'Р','R'},{'р','r'},
			{'С','S'},{'с','s'},
			{'Т','T'},{'т','t'},
			{'Ќ','}'},{'ќ',']'},
			{'У','U'},{'у','u'},
			{'Ф','F'},{'ф','f'},
			{'Х','H'},{'х','h'},
			{'Ц','C'},{'ц','c'},
			{'Ч','^'},{'ч','~'},
			{'Џ','x'},{'џ','x'},
			{'Ш','{'},{'ш','['},
			};
		return String.Concat( input.Select( c => ( map.ContainsKey( c ) ? map[c] : c ) ) );
	}

	public static String ConvertToKirilica( String input ) {
		try {
			Dictionary<char, char> map = new Dictionary<char, char>{
			{'A','А'},{'a','а'},
			{'B','Б'},{'b','б'},
			{'V','В'},{'v','в'},
			{'G','Г'},{'g','г'},
			{'D','Д'},{'d','д'},
			{'\\','Ѓ'},{'|','ѓ'},
			{'E','Е'},{'e','е'},
			{'@','Ж'},{'`','ж'},
			{'Z','З'},{'z','з'},
			{'Y','Ѕ'},{'y','ѕ'},
			{'I','И'},{'i','и'},
			{'J','Ј'},{'j','ј'},
			{'K','К'},{'k','к'},
			{'L','Л'},{'l','л'},
			{'Q','Љ'},{'q','љ'},
			{'M','М'},{'m','м'},
			{'N','Н'},{'n','н'},
			{'W','Њ'},{'w','њ'},
			{'O','О'},{'o','о'},
			{'P','П'},{'p','п'},
			{'R','Р'},{'r','р'},
			{'S','С'},{'s','с'},
			{'T','Т'},{'t','т'},
			{'}','Ќ'},{']','ќ'},
			{'U','У'},{'u','у'},
			{'F','Ф'},{'f','ф'},
			{'H','Х'},{'h','х'},
			{'C','Ц'},{'c','ц'},
			{'^','Ч'},{'~','ч'},
			{'X','Џ'},{'x','џ'},
			{'{','Ш'},{'[','ш'},
			};
			return String.Concat( input.Select( c => ( map.ContainsKey( c ) ? map[c] : c ) ) );
		}
		catch ( Exception ex ) {
			MessageBox.Show( ex.Message );
		}
		return "pp";
	}

	public static string Translate( String strIn, Control col ) {
		ResourceManager res = new ResourceManager( Application.ProductName + "Res.Resx", typeof( Control ).Assembly );
		return res.GetString( strIn, Application.CurrentCulture );
	}
}